package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbGroupJoinMapper;
import com.bwie.mapper.TbGroupRecordMapper;
import com.bwie.pojo.TbGroupActive;
import com.bwie.pojo.TbGroupJoin;
import com.bwie.pojo.TbGroupRecord;
import com.bwie.service.TbGroupActiveService;
import com.bwie.mapper.TbGroupActiveMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
* @author 31803
* @description 针对表【tb_group_active(拼团活动表)】的数据库操作Service实现
* @createDate 2025-07-27 20:52:04
*/
@Service
public class TbGroupActiveServiceImpl extends ServiceImpl<TbGroupActiveMapper, TbGroupActive>
    implements TbGroupActiveService{

    @Autowired
    TbGroupActiveMapper tbGroupActiveMapper;

    @Autowired
    TbGroupJoinMapper tbGroupJoinMapper;

    @Autowired
    HttpServletRequest request;

    @Autowired
    TbGroupRecordMapper tbGroupRecordMapper;

    @Override
    public R getActiveList(PageInfoVo pageInfoVo) {

        //构造分页数据
        Page<TbGroupActive> page = new Page<>(pageInfoVo.getPageNum(),pageInfoVo.getPageSize());
        //构造查询条件
        QueryWrapper<TbGroupActive> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            wrapper.lambda().like(TbGroupActive::getActiveName,pageInfoVo.getKeyWord());
        }
        wrapper.lambda().orderByDesc(TbGroupActive::getActiveId);
        Page<TbGroupActive> tbGroupActivePage = tbGroupActiveMapper.selectPage(page, wrapper);



        return R.OK(tbGroupActivePage);
    }

    @Override
    public R getAddActive(TbGroupActive tbGroupActive) {

        //设置审计字段
        tbGroupActive.setCreateBy("管理员");
        tbGroupActive.setCreateTime(new Date());
       //添加活动
        tbGroupActiveMapper.insert(tbGroupActive);

        return R.OK();
    }

    @Override
    public R getUpdateActive(TbGroupActive tbGroupActive) {

        //设置审计字段
        tbGroupActive.setUpdateBy("管理员");
        tbGroupActive.setUpdateTime(new Date());
        //修改活动
        tbGroupActiveMapper.updateById(tbGroupActive);

        return R.OK();
    }

    @Override
    public R getDeleteByIdActive(Integer activeId) {
        tbGroupActiveMapper.deleteById(activeId);



        return R.OK();
    }

    @Override
    public R getActive(Integer activeId) {

        return null;
    }

    @Override
    public R getGroupJoinList(PageInfoVo pageInfoVo) {
        //构造分页数据
        Page<TbGroupJoin> page = new Page<>(pageInfoVo.getPageNum(),pageInfoVo.getPageSize());
        //构造查询条件
        QueryWrapper<TbGroupJoin> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            wrapper.lambda().like(TbGroupJoin::getJoinName,pageInfoVo.getKeyWord());
        }
        wrapper.lambda().orderByDesc(TbGroupJoin::getJoinId);
        Page<TbGroupJoin> tbGroupJoinPage = tbGroupJoinMapper.selectPage(page, wrapper);
        List<TbGroupJoin> records = tbGroupJoinPage.getRecords();
        for (TbGroupJoin record : records) {
            if(record.getJoinImages()!=null &&!record.getJoinImages().equals("")){
                List<String> strings = JSON.parseArray(record.getJoinImages(), String.class);
                if(strings.size()>0&&strings!=null){
                    record.setImages(strings);
                }
            }

        }
        return R.OK(tbGroupJoinPage);
    }

    @Override
    public R addActiveJoin(TbGroupJoin tbGroupJoin) {

        //判断商品活动名称是否重复
        TbGroupJoin tbGroupJoin1 = tbGroupJoinMapper.selectOne(new QueryWrapper<TbGroupJoin>().lambda().eq(TbGroupJoin::getJoinName, tbGroupJoin.getJoinName()));
        if(tbGroupJoin1!=null){
            return R.ERROR(500,"该活动名称已经存在");
        }
        long begin = tbGroupJoin.getBeginTime().getTime();
        long end = tbGroupJoin.getEndTime().getTime();
        if(begin-end>0){
            return R.ERROR(500,"开始时间不能小于结束时间");
        }
        tbGroupJoin.setJoinImages(JSON.toJSONString(tbGroupJoin.getImages()));
        //添加商品活动
        tbGroupJoinMapper.insert(tbGroupJoin);

        return R.OK();
    }

    @Override
    public R updateActiveJoin(TbGroupJoin tbGroupJoin) {

        TbGroupJoin tbGroupJoin1 = tbGroupJoinMapper.selectById(tbGroupJoin.getJoinId());
        if(tbGroupJoin1==null){
            return R.ERROR(500,"该活动不存在");
        }
        if(!tbGroupJoin.getJoinName().equals(tbGroupJoin1.getJoinName())){
            TbGroupJoin tbGroupJoin2 = tbGroupJoinMapper.selectOne(new QueryWrapper<TbGroupJoin>().lambda().eq(TbGroupJoin::getJoinName, tbGroupJoin.getJoinName()));
            if(tbGroupJoin2!=null){
                return R.ERROR(500,"活动名称已经存在");
            }
        }
        List<String> images = tbGroupJoin.getImages();
        if(images!=null&&images.size()>0){
            tbGroupJoin.setJoinImages(JSON.toJSONString(images));
        }

        tbGroupJoinMapper.updateById(tbGroupJoin);


        return R.OK();
    }

    @Override
    public R deleteActiveJoin(Integer joinId) {
        tbGroupJoinMapper.deleteById(joinId);



        return R.OK();
    }

    @Override
    public R addActiveRecord(TbGroupJoin tbGroupJoin) {
        String token = request.getHeader("token");
        Integer userId = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody().get("userId", Integer.class);
        String userMobile = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody().get("userMobile", String.class);

        TbGroupRecord tbGroupRecord = new TbGroupRecord();
        tbGroupRecord.setGroupStatus(1);
        tbGroupRecord.setCreateBy(userMobile);
        tbGroupRecord.setCreateTime(new Date());
        tbGroupRecord.setBeginTime(tbGroupJoin.getBeginTime());
        tbGroupRecord.setEndTime(tbGroupJoin.getEndTime());
        tbGroupRecord.setJoinId(tbGroupJoin.getJoinId());
        tbGroupRecord.setJoinNum(tbGroupJoin.getGroupNum());
        tbGroupRecord.setTeamHeader(userMobile);
        tbGroupRecord.setTeamLimit(tbGroupJoin.getBuyMax());
        tbGroupRecordMapper.insert(tbGroupRecord);


        return R.OK();
    }

    @Override
    public R getRcordsList(PageInfoVo pageInfoVo) {
        //构造分页数据
        Page<TbGroupRecord> page = new Page<>(pageInfoVo.getPageNum(),pageInfoVo.getPageSize());
        //构造查询条件
        QueryWrapper<TbGroupRecord> wrapper = new QueryWrapper<>();

        wrapper.lambda().orderByDesc(TbGroupRecord::getGroupId);
        Page<TbGroupRecord> tbGroupRecordPage = tbGroupRecordMapper.selectPage(page, wrapper);
        return R.OK(tbGroupRecordPage);
    }
}




